<div class="main layui-clear">
	<h2 class="page-title">发表问题</h2>

	<div class="layui-form layui-form-pane">
		<form action="/jsluntan/topic/update/${topic.id!''}.html" method="post">
			<input type="hidden" name="id" value="${topic.id!''}">
			<div class="layui-form-item">
				<label for="L_title" class="layui-form-label">标题</label>
				<div class="layui-input-block">
					<input type="text" id="L_title" name="title" required
						lay-verify="required" autocomplete="off" class="layui-input"
						value="${topic.title!''}">
				</div>
			</div>
			<div class="layui-form-item layui-form-text">
				<div class="layui-input-block">
					<textarea id="content" name="content" required
						lay-verify="required" placeholder="请输入内容"
						class="layui-textarea fly-editor" style="height: 260px;">${topic.content!''}</textarea>
				</div>
				<label for="content" class="layui-form-label" style="top: -2px;">描述</label>
			</div>
			
	      	<#list topic.fileNames as fileName>
	      		<a href="${topic.fileUrls[fileName_index]}" target='_blank' id="file${fileName_index}">${fileName}</a>
	      		<img src="/jsluntan/images/delete.png" class="edit-file-delete" onclick="deleteFile('file${fileName_index}')" id="filem${fileName_index}">
	      	</#list>
			
	      	<div class="layui-form-item" style="display: none;">
	        	<label for="L_title" class="layui-form-label">附件名称</label>
	        	<div class="layui-input-block">
	          		<input type="text" id="L_filename" name="filename" value="${topic.filename!''}" autocomplete="off" class="layui-input">
	        	</div>
	      	</div>
	      	<div class="layui-form-item" style="display: none;">
	        	<label for="L_title" class="layui-form-label">附件链接</label>
	        	<div class="layui-input-block">
	          		<input type="text" id="L_fileurl" name="fileurl" value="${topic.fileurl!''}" autocomplete="off" class="layui-input">
	        	</div>
	      	</div>
	      	<div class="layui-form-item" style="display: none;">
	        	<label for="L_title" class="layui-form-label">删除的附件的名称</label>
	        	<div class="layui-input-block">
	          		<input type="text" id="L_deletedfilename" name="deletedfilename" value="${topic.deletedfilename!''}" autocomplete="off" class="layui-input">
	        	</div>
	      	</div>
	      	<div class="layui-form-item" style="display: none;">
	        	<label for="L_title" class="layui-form-label">删除的附件的链接</label>
	        	<div class="layui-input-block">
	          		<input type="text" id="L_deletedfileurl" name="deletedfileurl" value="${topic.deletedfileurl!''}" autocomplete="off" class="layui-input">
	        	</div>
	      	</div>
	      	
	      	<div class="layui-form-item">
				<input type="file" name="file" lay-type="file" class="layui-upload-file">
				<span id="files"></span>
	      	</div>
	      
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">所在类别</label>
					<div class="layui-input-block">
						<select lay-verify="required" name="cate">
							<option></option>
	              			<option value="问题咨询" selected="selected">问题咨询</option>
	              			<option value="改进建议">改进建议</option>
	              			<option value="产品介绍">产品介绍</option>
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">悬赏</label>
					<div class="layui-input-block">
						<select name="price">
							<option value="5"<#if topic.price == 5>selected</#if>>5</option>
							<option value="10"<#if topic.price == 10>selected</#if>>10</option>
							<option value="20"<#if topic.price == 20>selected</#if>>20</option>
							<option value="50"<#if topic.price == 50>selected</#if>>50</option>
							<option value="100"<#if topic.price == 100>selected</#if>>100</option>
						</select>
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<button class="layui-btn" lay-filter="*" lay-submit
					onclick="if(layedit){layedit.sync(index);}">立即发布</button>
			</div>
		</form>
	</div>
</div>
</div>
<script>
	var layedit  , index;
	layui.use(['form' , 'layedit'], function(){
  		var form = layui.form() ;
  		layedit = layui.layedit;
  		index = layedit.build('content'); //建立编辑器
	});
	

	layui.use('upload', function(){
  		layui.upload({
  			url: '/jsluntan/topic/upload.html',
  			before:function(res){
  				console.log("before res : " + res.value);
  			},
  			success: function(res){
  		    	console.log("success res : " + res.data.src); //上传成功返回值，必须为json格式
  		    	var b = document.getElementById("files");
  		    	b.innerHTML = b.innerHTML + "<a style='margin-left:15px;' href='"+res.data.path+"' target='_blank'> " + res.data.title + "</a>";
  		    	
  		    	var fname = document.getElementById("L_filename");
  		    	if(fname.value == null || fname.value == "") {
  		    		fname.value = res.data.title;
  		    	} else {
  		    		fname.value = fname.value + "," + res.data.title;
  		    	}

  		    	var furl = document.getElementById("L_fileurl");
  		    	if(furl.value == null || furl.value == "") {
  		    		furl.value = res.data.path;
  		    	} else {
  		    		furl.value = furl.value + "," + res.data.path;
  		    	}
  		  	}
  		});
  	});
	
	function deleteFile(id) {
		var index = id.substr(4);
		console.log("delete file index : " + index);
		console.log("delete file id : " + id);
		var file = document.getElementById(id);
		var filem = document.getElementById("filem" + index);
		file.style.display = "none";
		filem.style.display = "none";
		
		var filename = file.innerHTML;
		console.log("filename : " + filename);
		console.log("filename length : " + filename.length);

	    var fname = document.getElementById("L_filename");
	    var fdname = document.getElementById("L_deletedfilename");
		console.log("fname : " + fname.value);
		console.log("fname length : " + fname.value.length);
	    var indexn = fname.value.indexOf(filename);
		console.log("indexn : " + indexn);
	    if(indexn > -1) {
	    	if(indexn == 0) { // 第一个文件
	    		if(fname.value.length == filename.length) {
	    			fname.value = "";
	    		} else if (fname.value.length > filename.length) {
	    			fname.value = fname.value.substr(filename.length + 1)
	    		}
	    	} else if ((indexn + filename.length) == fname.value.length) { // 最后一个文件
    			fname.value = fname.value.substr(0, indexn - 1)
	    	} else {
	    		var head = fname.value.substr(0, indexn);
	    		console.log("head : " + head);
	    		var tail = fname.value.substr(indexn + filename.length + 1);
	    		console.log("tail : " + tail);
	    		fname.value = head + tail;
	    	}
	    	
	    	if(fdname.value == null || fdname.value == "") {
	    		fdname.value = filename;
		    } else {
		    	fdname.value = fdname.value + "," + filename;
		    }
	    }
		
		var fileurl = file.getAttribute("href");
		console.log("fileurl : " + fileurl);
		console.log("fileurl length : " + fileurl.length);

	    var furl = document.getElementById("L_fileurl");
	    var fdurl = document.getElementById("L_deletedfileurl");
		console.log("furl : " + furl.value);
		console.log("furl length : " + furl.value.length);
	    var indexu = furl.value.indexOf(fileurl);
		console.log("indexu : " + indexu);
	    if(indexu > -1) {
	    	if(indexu == 0) { // 第一个文件
	    		if(furl.value.length == fileurl.length) {
	    			furl.value = "";
	    		} else if (furl.value.length > fileurl.length) {
	    			furl.value = furl.value.substr(fileurl.length + 1)
	    		}
	    	} else if ((indexu + fileurl.length) == furl.value.length) { // 最后一个文件
	    		furl.value = furl.value.substr(0, indexu - 1)
	    	} else {
	    		var head = furl.value.substr(0, indexu);
	    		console.log("head : " + head);
	    		var tail = furl.value.substr(indexu + fileurl.length + 1);
	    		console.log("tail : " + tail);
	    		furl.value = head + tail;
	    	}

	    	if(fdurl.value == null || fdurl.value == "") {
	    		fdurl.value = fileurl;
		    } else {
		    	fdurl.value = fdurl.value + "," + fileurl;
		    }
	    }
	}

</script>